/*
 * Copyright (C) 2007-20089 Alessandro Melandri
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package net.melandri.jtextfileparser.beans;

import java.util.TreeMap;

import net.melandri.jtextfileparser.utils.StringUtils;

/**
 * @author Alessandro Melandri
 * @version 1.3
 */
public class Row {

	private int number;
	private String[] fields;
	private final String SEP;

	
	
	/**
	 * Constructor
	 * 
	 * @param sep Fields separator
	 * */
	public Row(String sep) {
		SEP = sep;
	}
	
	
	
	/**
	 * Constructor
	 * 
	 * @param textRow 	The row as a String
	 * @param seo 		Fields separator
	 * @param number 	THe number of the row
	 * 
	 * @since 1.3
	 * */
	public Row (String textRow, String sep, int number){
		SEP = sep;
		setFields(textRow.split(SEP));
		setNumber(number);
	}


	
	/**
	 * Returns an Array of String containing the values for the choosen fields
	 * 
	 * @param positions Array containing the fields id you want to retrieve.
	 */
	public String[] getFields(int[] positions) {

		String[] values = new String[positions.length];

		for (int i = 0; i < positions.length; i++) {
			values[i] = fields[positions[i]];
		}

		return values;
	}



	/**
	 * Returns the value of the specified field
	 * 
	 * @param index Field position. First field ha sposition 0
	 * 
	 * @return Return the value of the specified field
	 */
	public String get(int index) {
		return (String) fields[index];
	}



	/**
	 * Returns the number of fields in the row
	 * 
	 * @return Returns the number of fields in the row
	 * 
	 */
	public int length() {
		return fields.length;
	}



	/**
	 * Returns the row as a String
	 * 
	 * @deprecated Use getStringRow() instead.
	 * @return Returns the row as a String
	 */
	public String getSimpleRow() {
		return getStringRow();
	}



	/**
	 * Returns the row as a String
	 * 
	 * @since 1.2
	 * @return Returns the row as a String
	 */
	public String getStringRow() {
		String simpleRow = "";

		for (int i = 0; i < fields.length; i++) {

			if (i != (fields.length - 1))
				simpleRow += fields[i] + SEP;
			else
				simpleRow += fields[i];
		}

		return simpleRow;
	}



	/**
	 * Parses every field of the row and sobstitutes the chars specified. <br>
	 * Every element of the TreeMap should be: key = old char (String) ; value = new char (String);
	 * 
	 * @param chars TreeMap containg couples oldChar-newChar
	 */
	public void clean(TreeMap chars) {

		if (fields != null && fields.length > 0) {

			String[] fieldsTemp = new String[this.fields.length];

			for (int i = 0; i < this.fields.length; i++) {
				fieldsTemp[i] = StringUtils.substituteChars(fields[i], chars);
			}

			this.fields = fieldsTemp;
		}

	}


	/**
	 * @return Returns row's fields as a Strings array
	 */
	public String[] getFields() {
		return fields;
	}



	/**
	 * Sets the row's fields
	 * 
	 * @param fields Array of Strings
	 */
	public void setFields(String[] fields) {
		this.fields = fields;
	}



	/**
	 * @return The number of the row
	 */
	public int getNumber() {
		return number;
	}



	/**
	 * Sets the number of the row
	 * 
	 * @param number The number of the row.
	 */
	public void setNumber(int number) {
		this.number = number;
	}



	/**
	 * @return The fields separator for this row.
	 */
	public String getSEP() {
		return SEP;
	}
}
